<?php

namespace App\LeShua;

class LuShuaResponse
{
    private $content;
    public function __construct($content)
    {
        //解析xml
        $this->content = simplexml_load_string($content);
//        var_dump($content);
    }

    public function getResSuccess()
    {
        //resp_code 为0表示成功
        return $this->getContent('resp_code') == 0;
    }

    public function getResErrMsg()
    {
        return $this->getContent('resp_msg');
    }

    public function getResultCode()
    {
        return $this->getContent('result_code');
    }

    public function getErrCode()
    {
        return $this->getContent('error_code');
    }

    public function getErrorMsg()
    {
        return $this->getContent('error_msg');
    }

    public function getStatus()
    {
        return $this->getContent('status');
    }

    public function getPayWay()
    {
        return $this->getContent('pay_way');
    }

    public function getTradeNo()
    {
        return $this->getContent('leshua_order_id');
    }

    public function getPayTime()
    {
        return $this->getContent('pay_time');
    }

    public function getTransactionId()
    {
        return $this->getContent('out_transaction_id');
    }

    public function checkPaySuccess()
    {
        if ($this->getResultCode() != 0 || $this->getStatus() == 8)
            throw new \Exception($this->getErrorMsg());
    }

    public function getJSPay()
    {
        return json_decode($this->getContent('jspay_info'), true);
    }

    private function getContent($key, $first = true)
    {
        $res = $this->content->$key;

        if ($first && count($res) > 0)
            return $res[0];

        return $res;
    }
}
